Added quasi-random number generator and support for Rand<double>.#2149
Added quasi-random number generator and support for Rand<double>.#2149paulhoux wants to merge 1 commit intocinder:masterfrom
Conversation
…y Martin Roberts. See below for more details. http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ Also refactored the `Rand` class by adding a template argument. This allows the use of `double` and `long double` random values. By default it uses `float` values.
|
Really cool. And I bet you could use something like a combination of One thought - does it need to live in a separate file? Doesn't seem like much code, and I would think this, along with similar random number generators, could all live within cinder/Rand.h / cinder/Rand.cpp |
|
Actually I believe this quasi-random stuff should remain in its own |
|
Just a side note -- I've used the very good PCG random generator (http://www.pcg-random.org or https://github.com/imneme/pcg-cpp) in Cinder projects, when I needed random generation that was fast and produced the same results (for the same seeding) on different platforms (Mac and Windows, in my case). You don't get that with the standard library (for example That said, the Martin Roberts R2 "golden ratio" noise thing is also a nice addition, thanks! (-; |
| //! Returns a quasi-random float in the range [0.0f,1.0f). | ||
| T nextFloat() | ||
| { | ||
| static T sIrrational = T{ 1 } / phi( 1 ); |
There was a problem hiding this comment.
All these static sIrrational (and A,B,C variations later in the file) variables should be declared const.
Based on the excellent article by Martin Roberts:
http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/
The
QuasiRandclass returns quasi random values in the range [0, 1) which are evenly distributed across the full range. When used to generate points on a sphere, it looks like this:By comparison, fully random points would look like this:
The great thing about the algorithm is that it doesn't require you to know the number of values up front. Here's the same sphere after simply adding 4 times as many points:
The class is templated and uses
floatby default, but also supportsdoubleandlong double.I will add a sample later.
Also refactored the
Randclass by adding a template argument. This allows the use ofdoubleandlong doublerandom values. By default it usesfloatvalues.